
import time 
from traceback import print_exc 
from threading import Thread 
from Brain.operations.module import Module 


from Brain.camera.camera_device import CameraDevice
from Brain.camera.camera_device import TYPE_ENUM ## ids for colors of searched object

class CameraTask:
	def __init__(self, step=None, completed=True):
		self.step = step 
		self.is_completed = completed

class VisualCortex(Thread, Module):
	""" Camera logic
	"""
	FIXED_CAMERA_INDEX = 0
	FIXED_CAMERA_CONFIGFILE = 'boje.txt'
	
	def __init__(self, world, motorcortex):
		Thread.__init__(self)
		Module.__init__(self)
		## init fixed camera 
		self.fixed_camera = CameraDevice(self.FIXED_CAMERA_INDEX, self.FIXED_CAMERA_CONFIGFILE)
		self._world = world 
		self._motorcortex = motorcortex
		
		self._task = CameraTask()
		
		if self._world.our_color == 'red':
			self._our_color = TYPE_ENUM['RED']
		else:
			self._our_color = TYPE_ENUM['GREEN']
	
	def is_completed(self):
		return self._task.is_completed
	
	def start_task(self, step):
		""" go go go """
		self._task = CameraTask(step, False)
	
	def kill_task(self):
		""" free all resources ..
		"""
		self._task = CameraTask()
	
	def run(self):
		"""
		"""
		while self._working:
			if not self._task.is_completed:
				## do chase pack logic 
				data = self.fixed_camera.run(self._our_color)
				if data.status:
					x = data.x
					y = data.y
					print x, y, data.status, data.interval
				time.sleep(0.001)
			else:
				time.sleep(0.1)
	
	

if __name__ == '__main__':
	#print 'unit test missing ..'
	from Brain.world import WORLD
	
	world = WORLD()
	camera = VisualCortex(world, None)
	camera.start()
	camera.start_task(None)
	
	break_ = raw_input('press any key to break ..')
	
	camera.cleanup()










